package com.ruoyi.web.controller.example;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Anonymous
@Api(tags="文件管理")
@RestController
@RequestMapping("/file")
public class FileController {
    @Value("${ruoyi.profile}")
    private String uploadFolder;

    /*
     * 文件上传
     *
     * @param file 上传的文件
     * @return
     * */
    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public AjaxResult uploadFile(@RequestPart("file") MultipartFile file) {
        //1上传文件是否为空，如果是空的话，抛出异常
        if(file.isEmpty()) {
            throw new BizException(404, "文件不能为空");
        }
        //2判断服务器目录是否存在，不存在的话创建这个新服务器目录
        File folder = new File(uploadFolder);
        if(!folder.exists()) {
            folder.mkdirs();
        }
        //3在这个服务器目录下创建一个同名的空文件
        String fileName = file.getOriginalFilename();
        File destFile = new File(uploadFolder + "/" + fileName);
        //4上传的文件file的内容写入同名的空的文件夹
        try {
            file.transferTo(destFile);
        } catch (IOException e) {
            throw new BizException(500,"文件上传失败");
        }
        //5返回文件成功的消息

        return AjaxResult.success("文件上传成功");

    }
    @ApiOperation(value = "文件下载", notes = "下载服务器上文件到本地")
    @ApiImplicitParam(name = "name", value = "文件名", required = true, dataType = "string", paramType = "query",
            dataTypeClass = String.class)
    @GetMapping(value = "/download")
    public ResponseEntity<InputStreamResource> download(@RequestParam("name") String name) {
        FileSystemResource file = new FileSystemResource(uploadFolder + "/" + name);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"",
                file.getFilename()));
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");
        try {
            return ResponseEntity
                    .ok()
                    .headers(headers)
                    .contentLength(file.contentLength())
                    .contentType(MediaType.parseMediaType("application/octet-stream"))
                    .body(new InputStreamResource(file.getInputStream()));
        } catch (IOException e) {
            throw new BizException(500, "文件下载失败");
        }
    }
}




